#include "udpServer.hpp"
#include <memory>
#include <unordered_map>
#include <fstream>
#include <signal.h>
#include "onlineManager.hpp"


using namespace std;

Manager onlinemanager;

static const string dicttxt = "./dict.txt";
unordered_map<string,string> dictmap;

void load(){
    //璇绘暟鎹?
    ifstream read(dicttxt);
    string buffer;
    while(getline(read,buffer)){
        //澶勭悊鏁版嵁(鍒嗗壊)
        ssize_t cutpos = buffer.find(":");
        if(cutpos != string::npos){
            string english = buffer.substr(0,cutpos);
            if(dictmap.count(english) == 0){
                string chinese = buffer.substr(cutpos+1);
                dictmap.insert(make_pair(english,chinese));
            }
            
        }
    }
    //鎻掑叆鍒皍norderd_map
}

void Debugload(){
    for(const auto& x : dictmap){
        cout<<x.first<<" "<<x.second<<endl;
    }
}

void Usage(std::string proc){
    std::cout<<"Usage:\n"<<proc<<" Serverip "<<"Serverport\n"<<std::endl;
}

void dict(string ip,uint16_t port,string message,int sockfd){
    //灏嗘暟鎹鐞嗗悗鍙戝洖瀹㈡埛绔?
    string backinfo;
    if(dictmap.count(message)){
        backinfo = dictmap[message];
    }else{
        backinfo = "unkwon";
    }
    sockaddr_in server;
    socklen_t len= sizeof server;
    bzero(&server,sizeof server);
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = inet_addr(ip.c_str());
    server.sin_port = htons(port);
    sendto(sockfd,backinfo.c_str(),backinfo.size(),0,(sockaddr*)&server,len);
    
}

void handler(int signal){
    load();
}

void Commande(string ip,uint16_t port,string message,int sockfd){
    FILE* fp = popen(message.c_str(),"r");
    string ret;
    char buffer[gnum];
    while(fgets(buffer,sizeof buffer,fp)){
        //鏁版嵁宸茬粡鍒癰uffer閲屼簡
        ret += buffer;
    }
    sockaddr_in client;
    client.sin_addr.s_addr = inet_addr(ip.c_str());
    client.sin_family = AF_INET;
    client.sin_port = htons(port);
    socklen_t len = sizeof client;
    sendto(sockfd,ret.c_str(),ret.size(),0,(sockaddr*)&client,len);
    pclose(fp);
}

void routemassage(string ip,uint16_t port,string message,int sockfd){
    if(message == "register") onlinemanager.add(ip,port);
    if(message == "logout") onlinemanager.del(ip,port);

    if(message != "register" && onlinemanager.is_in(ip,port)){
        //澶勭悊杞彂涓氬姟
        onlinemanager.broadcastmessage(ip,port,message,sockfd);
    }else if(!onlinemanager.is_in(ip,port))
    {
        //鎻愮ず鍏舵敞鍐?
        if(message != "logout")
        {
            sockaddr_in client;
            client.sin_addr.s_addr = inet_addr(ip.c_str());
            client.sin_family = AF_INET;
            client.sin_port = htons(port);
            socklen_t len = sizeof client;
            string ret("请输入register注册");
            sendto(sockfd,ret.c_str(),ret.size(),0,(sockaddr*)&client,len);
        }
        
    }

}


int main(int argc,char* argv[]){
    load();
    signal(SIGQUIT,handler);
    //Debugload();
    if(argc != 2){
        Usage(argv[0]);
        exit(1);
    }
    uint16_t port = atoi(argv[1]);
    std::unique_ptr<udpServer> p(new udpServer(port,routemassage));
    p->initServer();
    p->start();
    return 0;
}